1
|
|
|
import { EditorState } from 'prosemirror-state'; |
2
|
|
|
import { EditorView } from 'prosemirror-view'; |
3
|
|
|
import { Schema, Node } from 'prosemirror-model'; |
4
|
|
|
|
5
|
|
|
import { tableEditing } from 'prosemirror-tables'; |
6
|
|
|
import getSpec from './schema'; |
7
|
|
|
import getKeymapPlugin from './plugins/Keymap/keymap'; |
8
|
|
|
import initializePublicAPI from './initializePublicAPI'; |
9
|
|
|
import MenuInitializer from './plugins/Menu/MenuInitializer'; |
10
|
|
|
import getNodeViews from './nodeviews'; |
11
|
|
|
|
12
|
|
|
initializePublicAPI(); |
13
|
|
|
|
14
|
|
|
window.Prosemirror.enableProsemirror = function enableProsemirror() { |
15
|
|
|
const schema = new Schema(getSpec()); |
16
|
|
|
|
17
|
|
|
const mi = new MenuInitializer(schema); |
18
|
|
|
|
19
|
|
|
// PLUGIN ORDER IS IMPORTANT! |
20
|
|
|
const plugins = [ |
21
|
|
|
mi.getMenuPlugin(), |
22
|
|
|
getKeymapPlugin(schema), |
23
|
|
|
tableEditing(schema), |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
const json = jQuery('#dw__editform').find('[name=prosemirror_json]').get(0); |
27
|
|
|
const view = new EditorView(document.querySelector('#prosemirror__editor'), { |
28
|
|
|
state: EditorState.create({ |
29
|
|
|
doc: Node.fromJSON(schema, JSON.parse(json.value)), |
30
|
|
|
schema, |
31
|
|
|
plugins, |
32
|
|
|
}), |
33
|
|
|
dispatchTransaction(tr) { |
34
|
|
|
console.log('run'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
view.updateState(view.state.apply(tr)); |
37
|
|
|
|
38
|
|
|
const spaces = 4; |
39
|
|
|
json.value = JSON.stringify(view.state.doc.toJSON(), null, spaces); // FIXME: no need to pretty print this! |
40
|
|
|
}, |
41
|
|
|
nodeViews: getNodeViews(), |
42
|
|
|
}); |
43
|
|
|
window.view = view; |
44
|
|
|
jQuery(window).on('scroll.prosemirror_menu', () => { |
45
|
|
|
const $container = jQuery('#prosemirror__editor'); |
46
|
|
|
const $menuBar = $container.find('.menubar'); |
47
|
|
|
const docViewTop = jQuery(window).scrollTop(); |
48
|
|
|
const containerTop = $container.offset().top; |
49
|
|
|
|
50
|
|
|
if (docViewTop > containerTop) { |
51
|
|
|
$menuBar.css('position', 'fixed'); |
52
|
|
|
} else { |
53
|
|
|
$menuBar.css('position', ''); |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
window.Prosemirror.destroyProsemirror = function destroyProsemirror() { |
59
|
|
|
if (window.view && typeof window.view.destroy === 'function') { |
60
|
|
|
window.view.destroy(); |
61
|
|
|
} |
62
|
|
|
jQuery(window).off('scroll.prosemirror_menu'); |
63
|
|
|
}; |
64
|
|
|
|